package com.dm.webCrawler.framework.auth;

import com.dm.webCrawler.framework.auth.mode.LoginUser;
import com.dm.webCrawler.framework.cache.CacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AuthCacheUtil {

	private String AUTH_CACHE_NAME="AuthCache";
	
	@Autowired
	private CacheUtils cacheUtils;
	
	/**
	 * 保存用户信息到缓存
	 * @param user
	 */
	public void saveUserToCache(LoginUser user) {
		cacheUtils.saveCache(AUTH_CACHE_NAME, user.getToken(), user);
	}
	
	/**
	 * 根据token获取缓存
	 * @param token tonken
	 * @param flush 是否更新缓存
	 * @return
	 */
	public LoginUser getUserBytoken(String token,boolean flush) {
		if(flush) {
			return cacheUtils.getCacheAndReflush(AUTH_CACHE_NAME, token, LoginUser.class);
		}else {
			return cacheUtils.getCache(AUTH_CACHE_NAME, token, LoginUser.class);
		}
		
	}
	
}
